package com.company.cloud.pay.modules.third.utils;

import java.util.Map;

import com.company.cloud.pay.core.enums.SignType;
import com.company.cloud.pay.core.kit.WxPayKit;
import com.company.cloud.pay.modules.api.request.PayOrderCloseRequest;
import com.company.cloud.pay.modules.api.request.PayOrderQueryRequest;
import com.company.cloud.pay.modules.api.request.PayOrderRequest;
import com.company.cloud.pay.modules.wxpay.WxPayApiConfig;
import com.company.cloud.pay.modules.wxpay.model.CloseOrderModel;
import com.company.cloud.pay.modules.wxpay.model.OrderQueryModel;
import com.company.cloud.pay.modules.wxpay.model.UnifiedOrderModel;

import cn.hutool.core.util.StrUtil;

public class WxPayApiParamKit {

    /**
     * 支付回调地址
     */
    public static String getNotifyUrl(WxPayApiConfig apiConfig) {
        String path = StrUtil.format("/pay/wx/{}/payNotify", apiConfig.getConfigId());
        return apiConfig.getDomain().concat(path);
    }

    /**
     * 退款回调地址
     */
    public static String refundNotifyUrl(WxPayApiConfig apiConfig) {
        String path = StrUtil.format("/pay/wx/{}/refundNotify", apiConfig.getConfigId());
        return apiConfig.getDomain().concat(path);
    }

    /**
     * 统一下单,参数校验
     */
    public static void validateUnifiedOrde(PayOrderRequest req) {

    }

    /**
     * 统一下单,支付参数
     */
    public static Map<String, String> getUnifiedOrderParam(WxPayApiConfig apiConfig, PayOrderRequest req) {
        validateUnifiedOrde(req);
        Map<String, String> params = UnifiedOrderModel.builder()
                .appid(apiConfig.getAppId())
                .mch_id(apiConfig.getMchId())
                .nonce_str(WxPayKit.generateStr())
                .body(req.getBody())
                .out_trade_no(req.getOutTradeNo())
                .total_fee(String.valueOf(req.getTotalFee()))
                .spbill_create_ip(req.getOpIp())
                .openid(req.getOpenid())
                .notify_url(WxPayApiParamKit.getNotifyUrl(apiConfig))
                .trade_type(req.getPtt().getTradeType())
                .attach(req.getAttach())
                .build()
                .createSign(apiConfig.getApiKey(), SignType.MD5);
        return params;
    }

    /**
     * 查询订单
     */
    public static Map<String, String> getOrderQueryParam(WxPayApiConfig apiConfig, PayOrderQueryRequest req) {
        Map<String, String> params = OrderQueryModel.builder()
                .appid(apiConfig.getAppId())
                .mch_id(apiConfig.getMchId())
                .nonce_str(WxPayKit.generateStr())
                .out_trade_no(req.getOutTradeNo())
                .build()
                .createSign(apiConfig.getApiKey(), SignType.MD5);
        return params;
    }

    /**
     * 关闭订单
     */
    public static Map<String, String> getCloseOrderParam(WxPayApiConfig apiConfig, PayOrderCloseRequest req) {
        Map<String, String> params = CloseOrderModel.builder()
                .appid(apiConfig.getAppId())
                .mch_id(apiConfig.getMchId())
                .nonce_str(WxPayKit.generateStr())
                .out_trade_no(req.getOutTradeNo())
                .build()
                .createSign(apiConfig.getApiKey(), SignType.MD5);
        return params;
    }


}
